package com.webex.util;

import java.awt.geom.Point2D;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.robochina.airobot.tank.Bot;
import org.robochina.math.MathUtils;

import com.webex.mirage.MirageRobot;

public class PatternMatchFire {

	// 保留历史纪录的最大长度
	private static final int HISTORY_LENGHT = 2000;
	// 用于匹配段的长度
	private static final int MATCH_LENGHT = 20;

	// 数组的当前索引
	private int currentIndex = 0;
	
	// 所有活着的对手的速度记录
	private Map allVelocity= new HashMap(5);
	// 所有活着的对手的角度记录
	private Map allHeading= new HashMap(5);
	
	
	/**
	 * 每一轮比赛开始时需要做的一些初始化工作。
	 */
	public void ini(){
		//游标复零
		currentIndex = 0;
		//清除上一轮的数据
		allVelocity.clear();
		allHeading.clear();
	}

	/**
	 * 记录所有对手的状态
	 */
	public void record(List bots){
		for (int i=0; i< bots.size(); i ++){
			Bot bot  = (Bot)bots.get(i);
			record(bot);
		}
		currentIndex++;
	}

	// 记录当前的机器人状态̬
	public void record(Bot bot) {
		double[] velocityRecord = (double[])allVelocity.get(bot.getName());
		double[] headingRecord = (double[])allHeading.get(bot.getName());
		
		if(velocityRecord == null){
			velocityRecord = new double[HISTORY_LENGHT];
			headingRecord = new double[HISTORY_LENGHT];
		}
		
		velocityRecord[currentIndex] = bot.getVelocity();
		headingRecord[currentIndex] = bot.getHeading();
		
		allVelocity.put(bot.getName(), velocityRecord);
		allHeading.put(bot.getName(), headingRecord);
	}

	// 计算最佳的匹配点
	public int getMatchIndex(Bot bot) {
		double beatSimilarity = Double.POSITIVE_INFINITY;
		int matchIndex = 0;
		// 这里取i<currentFrame-100是为了避免比较样本和被比较样本重复
		// 和留取足够的节点给递推未来坐标用
		for (int i = MATCH_LENGHT; i < currentIndex - MATCH_LENGHT; i++) {
			// 取10个样本节点计算相似度
			double similarity = 0;
			for (int j = 1; j <= MATCH_LENGHT; j++) {
				double[] velocityRecord = getVelocityRecord(bot);
				double[] headingRecord = getHeadingRecord(bot);
				similarity += Math.abs(velocityRecord[i - j]
						- velocityRecord[currentIndex - j]);
				similarity += Math.abs(headingRecord[i - j]
						- headingRecord[currentIndex - j]);
				// 加了权值得匹配度计算
				// similarity+=Math.abs(velocityRecord[i-j]-velocityRecord[currentIndex-j])/8;
				// similarity+=Math.abs(headingRecord[i-j]-headingRecord[currentIndex-j])/Math.PI;
			}
			// 记录最相似的相似度，以及对应的记录节点下标
			if (similarity < beatSimilarity) {
				matchIndex = i;
				beatSimilarity = similarity;
			}
		}
		return matchIndex;
	}

	// //�õ������λ�ￄ1�7
	// private Point2D getFirePoint(double power)
	// {
	// int matchIndex = getMatchIndex();
	// //Ԥ��λ��
	// Point2D firePoint = new MirageRobot().getFirstOpponent().getLocation();
	// int time = 0;
	// while(matchIndex+time<currentIndex)
	// {
	// double distance = MathUtils.distance(new MirageRobot().getLocation(),
	// firePoint);
	// if(distance/new MirageRobot().getBulletVelocity(power)<=time) break;
	// firePoint = MathUtils.nextPoint(firePoint,
	// headingRecord[matchIndex+time], velocityRecord[matchIndex+time]);
	// time++;
	// }
	// return firePoint;
	// }

	/**
	 * 得到开火的位置
	 */
	public Point2D getFirePoint(MirageRobot self, double power, Bot target) {
		// 预测位置
		int matchIndex = getMatchIndex(target);
		Point2D firePoint = self.getTarget().getLocation();
		int time = 0;
		
		double[] velocityRecord = getVelocityRecord(target);
		double[] headingRecord = getHeadingRecord(target);
		
		while (matchIndex + time < currentIndex) {
			double distance = MathUtils.distance(self.getLocation(), firePoint);
			if (distance / self.getBulletVelocity(power) <= time)
				break;
			firePoint = MathUtils.nextPoint(firePoint, headingRecord[matchIndex
					+ time], velocityRecord[matchIndex + time]);
			time++;
		}
		return firePoint;
	}
	
	/**
	 * 
	 * @return
	 */
	private double[] getVelocityRecord(Bot bot){
		return (double[])allVelocity.get(bot.getName());
	}
	
	private double[] getHeadingRecord(Bot bot){
		return (double[])allHeading.get(bot.getName());
	}
}
